//单利类

#ifndef SINGLETON_H
#define SINGLETON_H

#include <memory>
#include <mutex>

template<typename T>
class MagicSingleton
{
public:

    template<typename ...Args>
    static std::shared_ptr<T> GetInstance(Args&&... args)
    {
        static std::once_flag s_flag;
        std::call_once(s_flag, [&]() {
            m_pSington = std::shared_ptr<T>(new T(std::forward<Args>(args)...));
        });

        return m_pSington;
    }

    static void DesInstance()
    {
        if (m_pSington)
        {
            m_pSington.reset();
            m_pSington = nullptr;
        }
    }

private:
    explicit MagicSingleton();
    MagicSingleton(const MagicSingleton&) = delete;
    MagicSingleton& operator=(const MagicSingleton&) = delete;
    ~MagicSingleton();

private:
    static std::shared_ptr<T> m_pSington;
};

template<typename T>
std::shared_ptr<T> MagicSingleton<T>::m_pSington = nullptr;

#endif // SINGLETON_H
